local newt = reload("newt")
local calcPenalty = reload("plugins.cross.calcPenalty")
local calcInjuryLevel = reload("plugins.cross.calcInjuryLevel")

return function (arg)
    local ret = newt(0, 256)
    ---初始化四维属性
    ret.earth = arg.earth or 0
    ret.water = arg.water or 0
    ret.fire = arg.fire or 0
    ret.wind = arg.wind or 0

    ---初始异常属性
    ret.Poison = arg.Poison or 0
    ret.Sleep = arg.Sleep or 0
    ret.Stone = arg.Stone or 0
    ret.Drunk = arg.Drunk or 0
    ret.Confusion = arg.Confusion or 0
    ret.Amnesia = arg.Amnesia or 0


    ---初始化高级属性
    ret.ModCriticalMagic = 0
    ret.ModHitrateMagic = 0
    ret.ModAvoidMagic = 0
    ret.ModCounterMagic = 0

    ---初始化高级属性
    ret.ModCritical = arg.Critical or 0
    ret.ModCounter = arg.Counter or 0
    ret.ModHitrate = arg.Hitrate or 0
    ret.ModAvoid = arg.Avoid or 0

    ---初始化基础属性
    ret.ModAttack = 0
    ret.ModDefence = 0
    ret.ModAgility = 0
    ret.ModMagic = 0
    ret.ModCapture = 0
    ret.ModCharm = 0

    ---初始其他属性
    ret.FixLuck = arg.Luck
    ret.Tribe = arg.Tribe
    ret.Penalty = arg.Penalty or 0
    ret.EffectiveLevel = arg.level
    ---计算掉魂属性
    calcPenalty(ret)
    ret.MaxHp = math.floor(((ret.FixVital * 8) + (ret.FixStr * 2) + (ret.FixTough * 3) + (ret.FixQuick * 3) + (ret.FixMagic * 1)) * 0.01) + 20
    ret.MaxMp = math.floor(((ret.FixVital * 1) + (ret.FixStr * 2) + (ret.FixTough * 2) + (ret.FixQuick * 2) + (ret.FixMagic * 10)) * 0.01) + 20
    ---计算攻击力
    if arg.isPet then
        ret.AttackPower = math.floor((ret.FixVital * 0.2) + (ret.FixStr * 2.7) + (ret.FixTough * 0.3) + (ret.FixQuick * 0.3) + (ret.FixMagic * 0.2) * 0.01) + 20
    else
        ret.AttackPower = math.floor((ret.FixVital * 0.1) + (ret.FixStr * 2.0) + (ret.FixTough * 0.2) + (ret.FixQuick * 0.2) + (ret.FixMagic * 0.1) * 0.01) + 20
    end
    if arg.AttackRate then
        ret.AttackPower = ret.FixStr * arg.AttackRate // 10000
    end

    ---计算防御力
    if arg.isPet then
        ret.DefencePower = math.floor(((ret.FixVital * 0.2) + (ret.FixStr * 0.3) + (ret.FixTough * 3.0) + (ret.FixQuick * 0.3) + (ret.FixMagic * 0.2)) * 0.01) + 20
    else
        ret.DefencePower = math.floor(((ret.FixVital * 0.1) + (ret.FixStr * 0.2) + (ret.FixTough * 2.0) + (ret.FixQuick * 0.2) + (ret.FixMagic * 0.1)) * 0.01) + 20
    end
    if arg.DefenceRate then
        ret.DefencePower = ret.FixTough * arg.DefenceRate // 10000
    end

    ---计算敏捷
    ret.Agility = math.floor(((ret.FixVital * 0.1) + (ret.FixStr * 0.2) + (ret.FixTough * 0.2) + (ret.FixQuick * 2.0) + (ret.FixMagic * 0.1)) * 0.01) + 20
    if arg.AgilityRate then
        ret.Agility = ret.Agility * arg.AgilityRate // 10000
    end

    ---计算精神(魔力)
    ret.MagicPower = math.floor(((ret.FixVital * -0.3) + (ret.FixStr * -0.1) + (ret.FixTough * 0.2) + (ret.FixQuick * -0.1) + (ret.FixMagic * 0.8)) * 0.01) + 100
    if arg.MagicRate then
        ret.MagicPower = ret.MagicPower * arg.MagicRate // 10000
    end

    ---计算回复
    arg.Recovery = math.floor(((ret.FixVital * 0.8) + (ret.FixStr * -0.1) + (ret.FixTough * -0.1) + (ret.FixQuick * 0.2) + (ret.FixMagic * -0.3)) * 0.01) + 100
    if arg.RecoverRate then
        ret.Recovery = ret.Recovery * arg.RecoverRate // 10000
    end

    ---计算宠物忠诚度
    if arg.isPet then
        local Master = arg.Master
        ---正常忠诚
        local ai = (Master.level * Master.FixCharm) // (arg.level * arg.ModLoyalty) * 100
        ai = math.min(100, ai)
        ---附加忠诚
        local work = arg.VariableLoyality * 0.01
        work = math.min(100, work)
        work = math.max(0, work)

        ---计算总和
        ai = ai + work
        ai = math.min(100, ai)
        ai = math.max(0, ai)
        ret.FixLoyalty = math.min(ai, 60)
        ---调教技能
        local min = Master.minFixLoyalty or 0
        if min > ret.FixLoyalty then
            ret.FixLoyalty = min
        end
        local max = Master.maxFixLoyalty or 0
        if max < ret.FixLoyalty then
            ret.FixLoyalty = max
        end
    end

    if arg.BattleDuel == 1 then
        ret.MaxMp = ret.MaxMp // 2
    end

    ---宠物乘骑 todo

    ---固定血量
    if arg.fixMaxHp then
        ret.MaxHp = arg.fixMaxHp
    end


    ---受伤效果 todo
    local lv = calcInjuryLevel(arg)
    if 1 == lv then
        ---闪避-15%
        local old = ret.ModAvoid
        ret.ModAvoid = old - math.floor(old * 0.15)
    elseif 2 == lv then
        ---闪避-30%
        local old = ret.ModAvoid
        ret.ModAvoid = old - math.floor(old * 0.3)
        ---攻击-15%
        old = ret.AttackPower
        ret.AttackPower = old - math.floor(old * 0.15)
    elseif 3 == lv then
        ---闪避-60%
        local old = ret.ModAvoid
        ret.ModAvoid = old - math.floor(old * 0.6)
        ---攻击-30%
        old = ret.AttackPower
        ret.AttackPower = old - math.floor(old * 0.3)
        ---防御-15%
        old = ret.DefencePower
        ret.DefencePower = old - math.floor(old * 0.15)
    elseif 4 == lv then
        ---闪避-100%
        local old = ret.ModAvoid
        ret.ModAvoid = old - math.floor(old * 1)
        ---攻击-60%
        old = ret.AttackPower
        ret.AttackPower = old - math.floor(old * 0.6)
        ---防御-30%
        old = ret.DefencePower
        ret.DefencePower = old - math.floor(old * 0.3)
        ---必杀-50%
        old = ret.ModCritical
        ret.ModCritical = old - math.floor(old * 0.5)
    end
end

--[[

    arg = {

        ---四维属性
        earth = 地属性
        water = 水属性
        fire = 火属性
        wind = 风属性

        ---基础体质
        Vital = 体力
        Str = 力量
        Tough = 强度
        Quick = 速度
        Magic = 魔力

        ---异常属性
        Poison = 中毒
        Sleep = 睡眠
        Stone = 石化
        Drunk = 醉酒
        Confusion = 混乱
        Amnesia = 遗忘

        ---高级属性
        Critical = 必杀
        Counter = 反击
        HitRate = 命中
        Avoid = 闪避

        ---修正属性
        ModLoyalty = 宠物忠诚修正值
        VariableLoyality = 宠物忠诚附加
        DefenceRate = 防御加成-万分比
        AttackRate = 攻击加成-万分比
        AgilityRate = 敏捷加成-万分比
        MagicRate  = 精神加成-万分比
        RecoverRate = 回复加成-万分比


        ---其他属性
        Tribe = 种族
        Penalty = 调魂          0~10
        Luck = 幸运
        Fame = 声望
        Stamina = 耐力          0~100
        Intellingence = 智力    0~100
        Dex = 灵巧              0~100
        Charm = 魅力            0~100
        Adm = 魔攻              0
        Rss = 魔抗              0
        rankRate = 宠物排行加成
        level = 等级
        isPet = 是否宠物
        fixMaxHp = 固定最大血量
        Injury = 受伤属性

        ---升星数据
        starCore = {
            Vital = 体力
            Str = 力量
            Tough = 强度
            Quick = 速度
            Magic = 魔力
        }

        ---初始能力
        baseCore = {
            Vital = 体力
            Str = 力量
            Tough = 强度
            Quick = 速度
            Magic = 魔力
        }

        ---道具档值
        itemCore = {
            Vital = 体力
            Str = 力量
            Tough = 强度
            Quick = 速度
            Magic = 魔力
        }

        ---词条档值
        entryCore = {
            Vital = 体力
            Str = 力量
            Tough = 强度
            Quick = 速度
            Magic = 魔力
        }

        ---随机档值
        randCore = {
            Vital = 体力
            Str = 力量
            Tough = 强度
            Quick = 速度
            Magic = 魔力
        }

        ---已加pb点数
        useBpCore = {
            Vital = 体力
            Str = 力量
            Tough = 强度
            Quick = 速度
            Magic = 魔力
        }

        ---升级点数
        levelUpBP = 升级点数
    }

    ret = { 最终属性
        EffectiveLevel = 等级效果
        ModCriticalMagic = 必杀
        ModCritical = 必杀
        ModHitrateMagic = 命中
        ModHitrate = 命中
        ModAvoidMagic = 闪避
        ModAvoid = 闪避
        ModCounterMagic = 反击
        ModCounter = 反击
        ModAttack = 攻击
        ModDefence = 防御
        ModAgility = 精神
        ModMagic = 魔力
        ModCapture =
        ModCharm = 魅力
        Poison = 中毒
        Sleep = 睡眠
        Stone = 石化
        Drunk = 醉酒
        Confusion = 混乱
        Amnesia = 遗忘
        FixLuck = 幸运
        FixFame = 声望
        ModStamina = 耐力
        ModIntelligence = 智力
        ModDex = 灵巧
        FixCharm = 魅力
        Adm = 魔攻
        Rss = 魔抗
        Tribe = 种族
    }

]]
